<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%option explicit%>
<%
Dim RZ_IN
RZ_IN=1
%>
<!--#include file="../conn.inc.asp" -->
<!--#include file="admin_inc/admin.function.asp" -->
<!--#include file="admin_inc/admin.sqlfunction.asp" -->
<%
checkadmin(4)
'作者：山林客（ah_bill）
'博客：http://ruizhinet.blog.163.com
'网站：http://www.ruizhinet.cn
'本信息不会影响您网站的正常访问，请保留
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>修改路径</title>
<link type="text/css" href="style/style.css" rel="stylesheet" rev="stylesheet"/>
<script type="text/javascript" src="admin_js/common.js"></script> 
</head>
<body>
<form method="post" name="rzform" action="manage_imgpath.asp">
<table class="table_800" cellspacing="0" cellpadding="0">
<input type="hidden" name="act" value="0"/>	
<tr>
	<th colspan="2">替换数据库图片/Flash路径</th>
</tr>
<tr class="row">
	<td>旧上传目录</td>
	<td><input type="text" id="oldpath" name="oldpath" size="30"/> <span class="redstar">*</span> 例如：/upload/，必须以/开头和/结尾。</td>
</tr>
<tr class="row">
	<td>新上传目录</td>
	<td><input type="text" id="newpath" name="newpath" value="" size="30"/> <span class="redstar">*</span>例如：/ruizhi/upload/，必须以/开头和/结尾。</td>
</tr>
<tr class="row">
	<td colspan="2" class="tdleft">
说明：出于调用的方便，本系统在上传图片或Flash时采用了以“/”开头（相对于网站应用程序的根）的路径，本工具主要用于网站迁移的时候将各数据表中的旧路径批量替换为新路径。对于content字段，会根据以下正则表达式来匹配(src|href)(=[\'|\"])(旧路径\S*?)([\'|\"])，即会替换src=""、href=""的匹配部分。	
    </td>
</tr>
<tr class="row">
	<td colspan="2" class="tdleft">
	例子：
	<ul>
		<li>原来上传目录的绝地地址为：http://www.abc.com/upload/，则“旧上传目录”应填写：/upload/</li>
		<li>原来上传目录的绝地地址为：http://www.abc.com/ruizhi/upload/，则“旧上传目录”应填写：/ruizhi/upload/</li>
	</ul>
	新上传目录同理
    </td>
</tr>
<tr>
	<td colspan="2" class="tdcenter">
	<input type="button" class="btn" onclick="checkForm('imgpath');" value="保存"/>&nbsp;&nbsp;&nbsp;<input class="btn" type="reset" value="重置"/></td>
</tr>
</table>
</form>
<% 
dim oldpath,newpath,newpath2,counter
oldpath=HTMLEncode(trim(request("oldpath")))
newpath=HTMLEncode(trim(request("newpath")))
act=HTMLEncode(trim(request("act")))
dim act
if act<>"" And isNumber(act) Then
	If oldpath<>"" Then 
		if left(oldpath,1)<>"/" or right(oldpath,1)<>"/" then
			historygoback("路径必须以/开头，以/结尾")
		end If
	End If 
	If newpath<>"" Then 
		if left(newpath,1)<>"/" or  right(newpath,1)<>"/" then
			historygoback("路径必须以/开头，以/结尾")
		end If
	End If 
	act=CInt(act)
	If act=0 Then 
		redim img_arr(1)
		img_arr(0)="ad_imgswf"
		redim content_arr(1)
		content_arr(0)="ad_code"
		call ChangeTable("c_advertise", img_arr, content_arr, oldpath, newpath)	
	ElseIf act=1 Then 
		redim content_arr(1)
		content_arr(0)="content"
		call ChangeTable("c_article", "", content_arr, oldpath, newpath)	
	ElseIf act=2 Then 
		redim img_arr(1)
		img_arr(0)="logo"
		call ChangeTable("c_friendlink", img_arr, "", oldpath, newpath)	
	ElseIf act=3 Then 
		redim content_arr(1)
		content_arr(0)="content"
		call ChangeTable("c_job", "", content_arr, oldpath, newpath)	
	ElseIf act=4 Then 
		redim img_arr(1)
		img_arr(0)="photoimg"
		redim content_arr(1)
		content_arr(0)="content"
		call ChangeTable("c_photo", img_arr, content_arr, oldpath, newpath)	
	ElseIf act=5 Then 
		redim img_arr(1)
		img_arr(0)="productimg"
		redim content_arr(1)
		content_arr(0)="content"
		call ChangeTable("c_product", img_arr, content_arr, oldpath, newpath)
	ElseIf act=6 Then 
		redim img_arr(1)
		img_arr(0)="specialimg"
		redim content_arr(2)
		content_arr(0)="content_chn"
		content_arr(1)="content_eng"
		call ChangeTable("c_special", img_arr, content_arr, oldpath, newpath)	
	ElseIf act=7 Then 
		redim img_arr(1)
		img_arr(0)="typeimg"
		redim content_arr(2)
		content_arr(0)="content_chn"
		content_arr(1)="content_eng"
		call ChangeTable("c_type", img_arr, content_arr, oldpath, newpath)	
		WriteResult(session("result"))
		session("result")=""
	End If 
end if
function ChangeContentImg(content,oldpath,newpath) 
	if content<>"" and (not isnull(content)) then
	dim pattern,regEx,Matches,Match,result,foundval,newval
	pattern="(<img|<link|<embed|<script|<embed)([.\s\S]*?)(src|href|value)(=[\'|\""|\""""])("&oldpath&"\S*?)([\'|\""|\""""])([.\s\S]*?)(>)"
	Set regEx=new RegExp
	regEx.MultiLine = True
	regEx.Pattern=(pattern)
	regEx.IgnoreCase=True
	regEx.Global=True
	Set Matches=regEx.Execute(content)
	If err.number<>0 Then
		response.write "错误:" & err.description
		err.clear
		Exit Function
	End If
	If Matches.count <>0 Then
		counter=counter+Matches.count
		for each Match in Matches
			foundval=Match.value
			content=replace(foundval,oldpath,newpath)
		Next
	End If
	set regEx=Nothing
	end if
	ChangeContentImg=content
end Function

Function ImgReplace(content,oldpath,newpath)
	Dim regEx,result
	Set regEx=new RegExp
	regEx.Pattern=(""&oldpath&"")
	regEx.IgnoreCase=True
	regEx.Global=True
	result=regEx.Replace(content,newpath)
	ImgReplace = result
	Set regEx=Nothing
End Function

function ChangeImg(img,oldpath,newpath) 
	if img<>"" and (not isnull(img)) then
		If InStr(LCase(img),LCase(oldpath))>0 Then
			counter=counter+1
			ChangeImg=ImgReplace(img,oldpath,newpath)
		End If 
	else
		ChangeImg=img
	end if
end function
function ChangeTable(tablename, img_arr, content_arr, oldpath, newpath)
	counter=0
	dim rsa,sqla,i,j,tmp_img,tmp_content
	Set rsa = Server.CreateObject("ADODB.Recordset")
	if tablename="c_advertise" then
		sqla = "select * from c_advertise where ad_type=1 or ad_type=2"
	else
		sqla = "select * from ["&tablename&"]"
	end if
	rsa.open sqla,conn,1,3
	do while not rsa.eof
		if isarray(img_arr) then
			for i=0 To UBound(img_arr)-1
				tmp_img=rsa(img_arr(i))
				rsa(img_arr(i))=ChangeImg(tmp_img,oldpath,newpath)
			next
		end if
		if isarray(content_arr) then
			for j=0 To UBound(content_arr)-1
				tmp_content=rsa(content_arr(j))
				rsa(content_arr(j))=ChangeContentImg(tmp_content,oldpath,newpath)
			next
		end if
	rsa.update
	rsa.movenext
	loop
	rsa.close
	set rsa=nothing
	session("result")=session("result")&"<li>"&tablename&"表替换完成&nbsp;&nbsp;共替换"&counter&"个匹配内容</li>"
	If act<7 Then 
		Dim url
		url="manage_imgpath.asp?act="&(act+1)&"&oldpath="&oldpath&"&newpath="&newpath&""
		response.write("<div class=""tdcenter"">步骤"&(act+1)&"：<a href='"&url&"'><span style='color:blue;'>替换<span style='color:red'>"&tablename&"</span>表完成，即将替换下一张表。如果没有自动跳转，请点击这里</span></a></div>")
		response.write("<meta http-equiv='refresh' content='1;url="&url&"'>")
	End If 
end function
%> 
<%Function WriteResult(str)%>
<table  cellspacing="0" cellpadding="0" class="table_400">
<tr>
	<th>操作结果</th>
</tr>
<tr>
	<td style="padding-left:5px;">
    将<span style="color:red;"><%=oldpath%></span>替换为<span style="color:red;"><%=newpath%></span><br/>
    <ul><%=str%></ul>
    </td>
</tr>
</table>
<%End Function%>
</body>
</html>